<template>
  <div>
    <div class="mapbox">
      <div id="container"></div>
    </div>
    <div class="showmap">
      {{ locationbox }}
    </div>
  </div>
</template>
 
<script>
import AMapLoader from "@amap/amap-jsapi-loader";
// import AMapLoader from "@v"
window._AMapSecurityConfig = {
  securityJsCode: "07fd23ac351aae0b58037dbfae7a0df8",
};
export default {
  data() {
    return {
      map: null,
      geocoder: null,
      locationbox: undefined,
      marker: null,
    };
  },
  created() {
    this.initAMap();
  },
 
  methods: {
    getlocationname(locationname) {
      this.geoCode(locationname);
    },
    initAMap() {
      let that=this
      AMapLoader.load({
        key: "1bd720d8d49f06fc5dff51e92b0668ee", //设置您的key
        version: "2.0",
        plugins: [
          "AMap.ToolBar",
          "AMap.Driving",
          "AMap.Geolocation",
          "AMap.Geocoder",
        ],
        AMapUI: {
          version: "1.1",
          plugins: [],
        },
        Loca: {
          version: "2.0",
        },
      })
        .then((AMap) => {
          that.map = new AMap.Map("container", {
            // viewMode: "3D",
            zoom: 14,
            zooms: [2, 22],
            center: [119.814527, 28.878924],
            // mapStyle: "amap://styles/darkblue",
          });
          that.geocoder = new AMap.Geocoder({
            city: "全国", //城市设为北京，默认：“全国”
          });
          that.marker = new AMap.Marker({
            icon: new AMap.Icon({
              size: new AMap.Size(50, 50),
              image: "https://webapi.amap.com/theme/v1.3/markers/b/mark_bs.png",
              imageSize: new AMap.Size(25, 35),
              offset: new AMap.Pixel(0, 0), 
            }),
          });
          AMap.Event.addListener(that.marker, "dragend", function (e) {
            var newPosition = e.lnglat; // 获取标记拖拽后的新位置坐标
            // 处理拖拽后的逻辑...
       
            that.locationbox = newPosition; 
              
              that.updateloadte(newPosition)



          });
          that.locationbox = undefined;
        
        })
        .catch((e) => {
          console.log(e);
        });
    },
    geoCode(data) {
      var address = data;
      let that = this;
      that.geocoder.getLocation(address, function (status, result) {
        if (status === "complete" && result.geocodes.length) {
          var lnglat = result.geocodes[0].location;
          // document.getElementById('lnglat').value = lnglat;
          that.locationbox = lnglat;

          that.marker.setPosition(lnglat);

          that.marker.setDraggable(true);

          that.map.add(that.marker);

          that.map.setFitView(that.marker);
          that.updateloadte(lnglat);
 
        } else {
          console.log("根据地址查询位置失败");
        }
      });
    },
    updateloadte(nub) {
      this.$emit("haslocationmap", nub);
      console.log("根据地址查询位置失败");
    },
  },
};
</script>

<style scoped>
.mapbox {
  width: 650px;
  height: 750px;
  /* background: red; */
  border: 1px solid #cccccc65;
  box-shadow: 0 0 15px #ccc;
  border-radius: 5px;
}
#container {
  width: 100%;
  height: 100%;
}
.showmap {
  margin-top: 50px;
}
</style> 